package com.sentinel.controller;

import com.sentinel.entity.Order;
import com.sentinel.feign.client.UserClient;
import com.sentinel.feign.entity.User;
import com.sentinel.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @作者: 林江
 * @创建时间: 2023/12/15
 * @功能:
 */
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;

    /*--------------------------流控规则----------------------------------*/
    @GetMapping("getOrderInfo")
    public String getOrderInfo() {
        System.out.println(Thread.currentThread().getName() + "获取订单详情接口");
        return "获取订单详情成功";
    }

    @GetMapping("A")
    public String addOrderA() {
        orderService.addOrder();
        return Thread.currentThread().getName() + "保存订单成功";
    }

    @GetMapping("B")
    public String addOrderB() {
        orderService.addOrder();
        return Thread.currentThread().getName() + "保存订单成功";
    }


    @GetMapping("query")
    public String query() {
        return Thread.currentThread().getName() + "查询订单成功";
    }

    @GetMapping("save")
    public String save() {
        return Thread.currentThread().getName() + "保存订单成功";
    }

    /*---------------------------熔断规则-------------------------------*/
    @Autowired
    UserClient userClient;

    @GetMapping("/getOrder/{userId}")
    public Order getOrder(@PathVariable("userId") Integer userId) {
        User user = userClient.getUser(userId);

        Order order = new Order();
        if (userId == 1) {
            order = createOrder(1, 0561521, "华为手机订单");
        } else if (userId == 2) {
            order = createOrder(2, 345215, "小米手机订单");
        }
        order.setUser(user);
        return order;
    }

    public Order createOrder(Integer id, Integer code, String name) {
        Order order = new Order();
        order.setId(id);
        order.setCode(code);
        order.setName(name);
        return order;
    }
}
